﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;
using System.Reflection;

namespace E02
{
    /// <summary>
    /// 同步销售计量价格调整单
    /// </summary>
    public class A11 : ISYNChildGet
    {
        public void Code(ISYN handler, ref CodeResult result, SYNRight user, SerialType st, String baseNo, String billId)
        {
            try
            {
                result.Code = "E02.A11";
                List<E02A11P> ht = new List<E02A11P>();
                SqlParameter[] sps = new SqlParameter[1];
                sps[0] = new SqlParameter("@FID", SqlDbType.NVarChar, 50);
                sps[0].Value = billId;
                string sql = " select a.FID BillId,a.FBILLNO Number,a.FNOTE Remark,FDecimal2 AdjustPrice,FDecimal4 AdjustMoney,FComboBox AdjustType," +
                             " isnull(b.FName,'') Biller,case when FTime = '' then '1990-01-01 00:00:00' when FTime IS null then '1990-01-01 00:00:00' else FTime end BillTime," +
                             " isnull(c.FName,'') Checker,case when FTime1 = '' then '1990-01-01 00:00:00' when FTime1 IS null then '1990-01-01 00:00:00' else FTime1 end CheckDate " +
                             " ,(select FText5 from CardServer.dbo.KEW_DataBaseEntry where FNumber = " + baseNo + " ) FiUnitID from KEW_SALESPRICECHANGE a " +
                             " left join t_user b on a.FBiller = b.FUserID and b.FUserID <> 0 " +
                             " left join t_user c on c.FUserID = a.FUser and a.FUser <> 0 where a.FID = @FID ";
                sql = sql + "select a.FEntryID_SRC BillId,a.fid_src WBID,a.FBILLNO_SRC WBNo,d.F_102 CustID,e.FText ProductID,a.FDECIMAL3 Num," +
                            "a.FDECIMAL OrgPrice,a.FDECIMAL1 AdjustPrice,charindex('调整单' + c.FBILLNO,FTEXT13) NeedUpdate from KEW_SALESPRICECHANGEENTRY a " +
                            "left join KEW_SALESINFOENTRY b on a.FID_SRC = b.FID and a.FENTRYID_SRC = b.FENTRYID " +
                            "left join KEW_SALESPRICECHANGE c on a.FID = c.fid " +
                            "left join t_Organization d on d.FItemID = a.FBASE " +
                            "left join KEW_ProductsEntry e on e.FID = a.FBASE1 where a.FID = @FID ";
                Recordset rs = handler.Data.ExeProcedure(baseNo, sql, sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows.Count == 1)
                    {
                        DataRow row = rs.Data.Tables[0].Rows[0];

                        E02A11P e02A11P = new E02A11P();
                        for (int i = 0; i < rs.Data.Tables[0].Columns.Count; i++)
                        {
                            DataColumn dc = rs.Data.Tables[0].Columns[i];
                            ReflectUtil.setValue(e02A11P, dc.ColumnName, row[dc.ColumnName].ToString());
                        }

                        List<E02A11E> OrderList = new List<E02A11E>();

                        foreach (DataRow entry in rs.Data.Tables[1].Rows)
                        {
                            E02A11E e02A11E = new E02A11E();
                            for (int j = 0; j < rs.Data.Tables[1].Columns.Count; j++)
                            {
                                DataColumn dc = rs.Data.Tables[1].Columns[j];
                                ReflectUtil.setValue(e02A11E, dc.ColumnName, entry[dc.ColumnName].ToString());
                            }
                            OrderList.Add(e02A11E);
                        }
                        e02A11P.ListAdjustPrice = handler.Json.ClassToJson(OrderList);
                        ht.Add(e02A11P);
                        result.Content = handler.Json.ClassToJson(ht);
                    }
                    else
                    {
                        result.Result = EnumResult.None;
                    }
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("销售账户数据提取错误:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("销售账户数据提取错误:{0}", ex.Message);
            }
        }

    }

    /// <summary>
    /// 销售计量价格调整
    /// </summary>
    public class E02A11P
    {
        public String BillId = String.Empty; //单据ID
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 备注
        /// </summary>
        public String Remark;
        /// <summary>
        /// 调整单价
        /// </summary>
        public String AdjustPrice;
        /// <summary>
        /// 调整金额
        /// </summary>
        public String AdjustMoney;
        /// <summary>
        /// 调整方式
        /// </summary>
        public String AdjustType;
        /// <summary>
        /// 制单人
        /// </summary>
        public String Biller;
        /// <summary>
        /// 制单时间
        /// </summary>
        public String BillTime;
        /// <summary>
        /// 审核人
        /// </summary>
        public String Checker;
        /// <summary>
        /// 审核日期
        /// </summary>
        public String CheckDate;
        /// <summary>
        /// 调价单
        /// </summary>
        public String ListAdjustPrice;

        /// <summary>
        /// EAS财务组织
        /// </summary>
        public String FiUnitID;
    }
    /// <summary>
    /// 销售计量价格调整分录
    /// </summary>
    public class E02A11E
    {
        public String BillId = String.Empty; //单据ID
        /// <summary>
        /// 过磅单内码
        /// </summary>
        public String WBID;
        /// <summary>
        /// 过磅单编号
        /// </summary>
        public String WBNo;
        /// <summary>
        /// 客户ID
        /// </summary>
        public String CustID;
        /// <summary>
        /// 物料ID
        /// </summary>
        public String MaterialID;
        /// <summary>
        /// 产品ID
        /// </summary>
        public String ProductID;
        /// <summary>
        /// 数量
        /// </summary>
        public Decimal Num;
        /// <summary>
        /// 原始单价
        /// </summary>
        public Decimal OrgPrice;
        /// <summary>
        /// 调整单价
        /// </summary>
        public Decimal AdjustPrice;
        /// <summary>
        /// 是否需要更新过磅单单价
        /// </summary>
        public Int32 NeedUpdate;


    }
}
